package com.sgb.kjwl.view.widget;

import android.content.Context;
import android.util.SparseBooleanArray;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;

import com.sgb.kjwl.R;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.request.MaterialFilterJson;
import com.sgb.kjwl.model.entity.supplier3.DictEntity3;
import com.sgb.kjwl.model.entity.user.TwoOrThreeLevelEntity;
import com.sgb.kjwl.view.ui.adapter.material.MaterialFilterAdapter3;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.customview.MaxRecyclerView;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.util.MToast;
import com.swgk.core.util.ScaleUtils;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author 杨浩
 */
public class MatterChooseMaterialFliterPopWindow3 extends BasePopWindow {
    private LinearLayout rootLayout;
    /**
     * 分类
     */
    private LinearLayout typeContainerone;
    /**
     * 类别
     */
    private LinearLayout typeContainer;
    /**
     * 品名
     */
    private LinearLayout nameContainer;
    /**
     * 品牌
     */
    private LinearLayout brandContainer;
    /**
     * 距离
     */
    private LinearLayout distanceContainer;
    /**
     * 价格
     */
    private LinearLayout priceContainer;
    private TextView areaView;
    private TextView brandView;
    private TextView distanceView;
    private TextView priceView;
    private View areaLine, typeLine, brandLine, nameLine, distanceLine, attrLine;
    private TextView okView;
    private TextView resetView;
    private MaxRecyclerView areaRecyclerView;
    private MaxRecyclerView typeRecyclerViewOne;
    private MaxRecyclerView typeRecyclerView;
    private MaxRecyclerView nameRecyclerView;
    private MaxRecyclerView brandRecyclerView;
    private MaxRecyclerView distanceRecyclerView;
    private MaxRecyclerView priceRecyclerView;
    private MaterialFilterAdapter3 typeAdapterone;
    private MaterialFilterAdapter3 typeAdapter;
    private MaterialFilterAdapter3 nameAdapter;
    private MaterialFilterAdapter3 brandAdapter;
    private MaterialFilterAdapter3 areaAdapter;
    private MaterialFilterAdapter3 distanceAdapter;
    private MaterialFilterAdapter3 priceAdapter;
    private FilterResultClick filterResultClick;
    private MaterialFilterJson materialFilterJson;
    private List<String> areaList;
    private List<String> materKeyList;
    private List<String> materValueList;
    private List<String> tempMaterKeyList;
    private List<String> tempMaterValueList;
    private List<MaterialFilterAdapter3> adapterList;
    private List<String> b = new ArrayList<>();

    private String mAreaCode;

    public interface FilterResultClick {
        void result(MaterialFilterJson materialFilterJson, List<DictEntity3> data, SparseBooleanArray checkBooleanArray);

        /**
         * 返回分类id
         * @param id
         */
        void queryTypeClassid(String id);

        void queryTypeChild(String id);

        void queryNameChild(String id);
    }

    public void setFilterResultClick(FilterResultClick filterResultClick) {
        this.filterResultClick = filterResultClick;
    }

    public MatterChooseMaterialFliterPopWindow3(Context context) {
        super(context, DIR_RIGHT_MATCH_HEIGHT);
    }

    @Override
    protected int popLayout() {
        return R.layout.popwin_material_fliter3;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        rootLayout = holder.getPop().findViewById(R.id.root_layout);
        typeContainerone = holder.getPop().findViewById(R.id.type_container_one);
        typeContainer = holder.getPop().findViewById(R.id.type_container);
        nameContainer = holder.getPop().findViewById(R.id.name_container);
        brandContainer = holder.getPop().findViewById(R.id.brand_container);
        distanceContainer = holder.getPop().findViewById(R.id.distance_container);
        priceContainer = holder.getPop().findViewById(R.id.price_container);
        okView = holder.getPop().findViewById(R.id.ok_view);
        resetView = holder.getPop().findViewById(R.id.reset_view);
        areaView = holder.getPop().findViewById(R.id.area_view);
        areaLine = holder.getPop().findViewById(R.id.area_line);
        typeLine = holder.getPop().findViewById(R.id.type_line);
        nameLine = holder.getPop().findViewById(R.id.name_line);
        brandView = holder.getPop().findViewById(R.id.brand_view);
        brandLine = holder.getPop().findViewById(R.id.brand_line);
        distanceView = holder.getPop().findViewById(R.id.distance_view);
        distanceLine = holder.getPop().findViewById(R.id.distance_line);
        attrLine = holder.getPop().findViewById(R.id.attr_line);
        priceView = holder.getPop().findViewById(R.id.price_view);
        areaRecyclerView = holder.getPop().findViewById(R.id.area_list);
        typeRecyclerView = holder.getPop().findViewById(R.id.type_list);
        typeRecyclerViewOne = holder.getPop().findViewById(R.id.type_list_one);
        nameRecyclerView = holder.getPop().findViewById(R.id.name_list);
        brandRecyclerView = holder.getPop().findViewById(R.id.brand_list);
        distanceRecyclerView = holder.getPop().findViewById(R.id.distance_list);
        priceRecyclerView = holder.getPop().findViewById(R.id.price_list);
        areaRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        typeRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        typeRecyclerViewOne.setLayoutManager(new GridLayoutManager(mContext, 3));
        nameRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        brandRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        distanceRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        priceRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        areaAdapter = new MaterialFilterAdapter3(mContext, null);
        areaAdapter.setItemSelect(true);
        areaAdapter.setDefItem(-1);
        areaAdapter.setOnItemMultiClick((dictEntity3s, position) -> {
            List<String> mStr = new ArrayList<>();
            for (DictEntity3 dictEntity3 : dictEntity3s) {
                mStr.add(dictEntity3.getId());
            }
            materialFilterJson.setAreaList(mStr);
        });

        areaRecyclerView.setAdapter(areaAdapter);
        typeAdapter = new MaterialFilterAdapter3(mContext, null);
        typeAdapter.setItemSelect(true);
        typeAdapter.setDefItem(-1);
        typeAdapter.setOnItemClick(new AdapterOnItemClick<DictEntity3>() {
            @Override
            public void onItemClick(DictEntity3 materialFilterEntity, int position) {
                if (filterResultClick != null) {
                    setNameData(new ArrayList<>());
                    setSpecData(new ArrayList<>());
                    setBrandData(new ArrayList<>());
                    if ("全部".equals(materialFilterEntity.getName())) {
                        materialFilterJson.setCategoryId("");
                        filterResultClick.queryTypeChild("");
//                        nameContainer.setVisibility(View.GONE);
//                        brandContainer.setVisibility(View.GONE);
//                        setSpecData(new ArrayList<>());
                    } else {
//                        nameContainer.setVisibility(View.VISIBLE);
                        materialFilterJson.setCategoryId(materialFilterEntity.getId());
                        filterResultClick.queryTypeChild(materialFilterEntity.getId());
                    }
                }

            }
        });
        typeRecyclerView.setAdapter(typeAdapter);

        typeAdapterone = new MaterialFilterAdapter3(mContext, null);
        typeAdapterone.setItemSelect(true);
        typeAdapterone.setDefItem(-1);
        typeAdapterone.setOnItemClick(new AdapterOnItemClick<DictEntity3>() {
            @Override
            public void onItemClick(DictEntity3 materialFilterEntity, int position) {
                if (filterResultClick != null) {
                    setTypeData(new ArrayList<>());
                    setNameData(new ArrayList<>());
                    setSpecData(new ArrayList<>());
                    setBrandData(new ArrayList<>());
                    if ("全部".equals(materialFilterEntity.getName())) {
                        materialFilterJson.setmCid1("");
                        materialFilterJson.setCategoryId("");
                        filterResultClick.queryTypeClassid("");
                    } else {
                        materialFilterJson.setmCid1(materialFilterEntity.getId());
                        filterResultClick.queryTypeClassid(materialFilterEntity.getId());
                    }
                }

            }
        });
        typeRecyclerViewOne.setAdapter(typeAdapterone);
        nameAdapter = new MaterialFilterAdapter3(mContext, null);
        nameAdapter.setItemSelect(true);
        nameAdapter.setDefItem(0);
        nameAdapter.setOnItemClick(new AdapterOnItemClick<DictEntity3>() {
            @Override
            public void onItemClick(DictEntity3 materialFilterEntity, int position) {
                if (filterResultClick != null) {
                    setSpecData(new ArrayList<>());
                    setBrandData(new ArrayList<>());

                    if ("全部".equals(materialFilterEntity.getName())) {
//                        brandContainer.setVisibility(View.GONE);
                        materialFilterJson.setItemId("");
                    } else {
                        materialFilterJson.setItemId(materialFilterEntity.getId());
                        filterResultClick.queryNameChild(materialFilterEntity.getId());
                    }
                }
            }
        });
        nameRecyclerView.setAdapter(nameAdapter);
        brandAdapter = new MaterialFilterAdapter3(mContext, null);
        brandAdapter.setItemSelect(true);
        brandAdapter.setDefItem(-1);
        brandAdapter.setOnItemClick(new AdapterOnItemClick<DictEntity3>() {
            @Override
            public void onItemClick(DictEntity3 materialFilterEntity, int position) {
                b.clear();
                if (!"".equals(materialFilterEntity.getId())) {
                    b.add(materialFilterEntity.getId());
                }
                materialFilterJson.setBrandId(b);
                materialFilterJson.setBrandName(materialFilterEntity.getName());
            }
        });
        brandRecyclerView.setAdapter(brandAdapter);
        distanceAdapter = new MaterialFilterAdapter3(mContext, null);
        distanceAdapter.setItemSelect(true);
        distanceAdapter.setDefItem(0);
        distanceAdapter.setOnItemClick(new AdapterOnItemClick<DictEntity3>() {
            @Override
            public void onItemClick(DictEntity3 materialFilterEntity, int position) {
                materialFilterJson.setDistanceId(materialFilterEntity.getId());
                materialFilterJson.setDistance(materialFilterEntity.getName());
            }
        });
        distanceRecyclerView.setAdapter(distanceAdapter);
        priceAdapter = new MaterialFilterAdapter3(mContext, null);
        priceAdapter.setItemSelect(true);
        priceAdapter.setDefItem(-1);
        priceAdapter.setOnItemClick(new AdapterOnItemClick<DictEntity3>() {
            @Override
            public void onItemClick(DictEntity3 materialFilterEntity, int position) {
                materialFilterJson.setPriceSort(Integer.parseInt(materialFilterEntity.getId()));
            }
        });
        priceRecyclerView.setAdapter(priceAdapter);
        materialFilterJson = new MaterialFilterJson();
        areaList = new ArrayList<>();
        materKeyList = new ArrayList<>();
        materValueList = new ArrayList<>();
        tempMaterKeyList = new ArrayList<>();
        tempMaterValueList = new ArrayList<>();
        adapterList = new ArrayList<>();
        okView.setOnClickListener(v -> {
            dissMiss();
            tempMaterKeyList.clear();
            tempMaterValueList.clear();
            for (int i = 0; i < materValueList.size(); i++) {
                if (!"".equals(materValueList.get(i))) {
                    tempMaterValueList.add(materValueList.get(i));
                    tempMaterKeyList.add(materKeyList.get(i));
                }
            }
            materialFilterJson.setMaterKey(tempMaterKeyList);
            materialFilterJson.setMaterValue(tempMaterValueList);
            if (filterResultClick != null) {
                filterResultClick.result(materialFilterJson, areaAdapter.getCheckDatas(), areaAdapter.getCheckList());
            }
        });
        resetView.setOnClickListener(v -> resetData());
    }

    private void resetData() {
        for (MaterialFilterAdapter3 adapter : adapterList) {
            adapter.setDefItem(0);
        }
        for (int i = 0; i < materValueList.size(); i++) {
            materValueList.set(i, "");
        }
        materialFilterJson.setMaterKey(new ArrayList<>());
        materialFilterJson.setMaterValue(new ArrayList<>());
        materialFilterJson.setBrandId(new ArrayList<>());
        materialFilterJson.setItemId("");
        materialFilterJson.setCategoryId("");
        materialFilterJson.setDistanceId("");
        materialFilterJson.setDistance("");
        materialFilterJson.setPriceSort(0);
        materialFilterJson.setAreaId("");
        materialFilterJson.setmCid1("");
        typeAdapter.setDefItem(0);
        nameAdapter.setDefItem(0);
        brandAdapter.setDefItem(-1);
        distanceAdapter.setDefItem(0);
        priceAdapter.setDefItem(-1);
        setNameData(new ArrayList<>());
        setSpecData(new ArrayList<>());
        setBrandData(new ArrayList<>());
        areaList.clear();
        areaAdapter.resetCheckDatas();
        areaAdapter.setDefItem(0);
    }

    /**
     * 地区
     *
     * @param data
     */
    public void setAreaData(List<DictEntity3> data) {
        if (data.size() == 0) {
            areaView.setVisibility(View.GONE);
            areaLine.setVisibility(View.GONE);
        } else {
            areaView.setVisibility(View.VISIBLE);
            areaLine.setVisibility(View.VISIBLE);
        }
        areaAdapter.setDatas(data);
        areaAdapter.setDefItem(0);
        materialFilterJson.setAreaId(data.get(0).getId());
    }

    public void setAreaDefault(int position, String id) {
        areaAdapter.setDefItem(position);
        materialFilterJson.setAreaId(id);
    }

    /**
     * 商品一级分类
     */
    public void setGoodsClassList(List<DictEntity3> data){
        typeAdapterone.setDatasMayNull(data);
        if (data == null || data.size() == 0) {
            typeContainerone.setVisibility(View.GONE);
            materialFilterJson.setCategoryId("");
        } else {
            typeContainerone.setVisibility(View.VISIBLE);
            typeAdapterone.setDefItem(0);
            materialFilterJson.setmCid1(data.get(0).getId());
        }
    }

    /**
     * 商品类别
     *
     * @param data 类别
     */
    public void setTypeData(List<DictEntity3> data) {
        typeAdapter.setDatasMayNull(data);
        if (data == null || data.size() == 0) {
            typeContainer.setVisibility(View.GONE);
            materialFilterJson.setCategoryId("");
        } else {
            typeContainer.setVisibility(View.VISIBLE);
            typeAdapter.setDefItem(0);
            materialFilterJson.setCategoryId(data.get(0).getId());
        }
    }
    public void setClassDefault(int position, String id) {
        typeAdapterone.setDefItem(position);
        materialFilterJson.setmCid1(id);
    }

    public void setTypeDefault(int position, String id) {
        typeAdapter.setDefItem(position);
        materialFilterJson.setCategoryId(id);
    }

    /**
     * 品名
     *
     * @param data
     */
    public void setNameData(List<DictEntity3> data) {
        nameAdapter.setDatasMayNull(data);
        if (data == null || data.size() == 0) {
            nameContainer.setVisibility(View.GONE);
            materialFilterJson.setItemId("");
        } else {
            nameContainer.setVisibility(View.VISIBLE);
            nameAdapter.setDefItem(0);
            materialFilterJson.setItemId(data.get(0).getId());
        }
    }

    public void setNameDefault(int position, String id) {
        nameAdapter.setDefItem(position);
        materialFilterJson.setItemId(id);
    }

    /**
     * 品牌
     *
     * @param data
     */
    public void setBrandData(List<DictEntity3> data) {

        brandAdapter.setDatasMayNull(data);
        if (data == null || data.size() == 0) {
            brandContainer.setVisibility(View.GONE);

        } else {
            brandContainer.setVisibility(View.VISIBLE);
            // setBrandDefault(0, data.get(0).getId());
        }
        brandAdapter.setDefItem(0);
        b.clear();
        materialFilterJson.setBrandId(b);
    }

    public void setBrandDefault(int position, String id) {
        brandAdapter.setDefItem(position);
        b.clear();
        b.add(id);
        materialFilterJson.setBrandId(b);
    }

    /**
     * 距离
     *
     * @param data
     */
    public void setDistanceData(List<DictEntity3> data) {
        if (data == null || data.size() == 0) {
            distanceContainer.setVisibility(View.GONE);
        } else {
            distanceContainer.setVisibility(View.VISIBLE);
        }
        distanceAdapter.setStringName(1);
        distanceAdapter.setDatas(data);
    }

    public void setDistanceDefault(int position, String id) {
        distanceAdapter.setDefItem(position);
        materialFilterJson.setDistanceId(id);
    }

    /**
     * 价格排序
     *
     * @param data 价格
     */
    public void setPriceData(List<DictEntity3> data) {
        if (data == null || data.size() == 0) {
            priceContainer.setVisibility(View.GONE);
        } else {
            priceContainer.setVisibility(View.VISIBLE);
        }
        priceAdapter.setDatas(data);
    }

    /**
     * 属性
     *
     * @param data
     */
    public void setSpecData(List<DictEntity3> data) {
        if (data.size() == 0) {
            rootLayout.setVisibility(View.GONE);
            attrLine.setVisibility(View.GONE);
        } else {
            rootLayout.setVisibility(View.VISIBLE);
            attrLine.setVisibility(View.VISIBLE);
        }
        adapterList.clear();
        rootLayout.removeAllViews();
        materKeyList.clear();
        materValueList.clear();
        for (int i = 0; i < data.size(); i++) {
            materKeyList.add(data.get(i).getName());
            materValueList.add("");
            addTitleView(data.get(i).getName());
            addRecyclerView(data.get(i).getDatas(), i);
        }
    }

    private void addTitleView(String title) {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.gravity = Gravity.CENTER_VERTICAL;
        layoutParams.setMargins(ScaleUtils.px2dip(mContext, 11), ScaleUtils.px2dip(mContext, 10), ScaleUtils.px2dip(mContext, 10), ScaleUtils.px2dip(mContext, 11));
        TextView textView = new TextView(mContext);
        textView.setText(title);
        textView.setTextSize(14);
        textView.setTextColor(mContext.getResources().getColor(R.color.color_999999));
        rootLayout.addView(textView, layoutParams);
    }

    private void addRecyclerView(List<DictEntity3> data, final int index) {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        MaterialFilterAdapter3 childAdapter = new MaterialFilterAdapter3(mContext, null);
        childAdapter.setItemSelect(true);
        layoutParams.setMargins(ScaleUtils.px2dip(mContext, 10), ScaleUtils.px2dip(mContext, 0), ScaleUtils.px2dip(mContext, 10), ScaleUtils.px2dip(mContext, 0));
        childAdapter.setDefItem(0);
        childAdapter.setIndex(index);
        adapterList.add(childAdapter);
        MaxRecyclerView recyclerView = new MaxRecyclerView(mContext);
        recyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        recyclerView.setAdapter(childAdapter);
        childAdapter.setDatas(data);
        childAdapter.setOnItemClick(new AdapterOnItemClick<DictEntity3>() {
            @Override
            public void onItemClick(DictEntity3 materialFilterEntity, int position) {
                materValueList.set(childAdapter.getIndex(), materialFilterEntity.getName());
            }
        });
        rootLayout.addView(recyclerView, layoutParams);
    }

    public void showListVisible(int position) {
        if (position != 0) {
            nameContainer.setVisibility(View.VISIBLE);
        }
    }


    /**
     * 获取二三级分裂
     */

    @Override
    public void dismiss() {
        super.dismiss();
        if (onDisMiss != null) {
            onDisMiss.disMiss();
        }
    }
}
